{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2D Iteration\n",
    "\n",
    "In this demonstration, you'll see how to iterate through a 2D array used nested loops, and we'll be using our iterative skills to locate trees in this world!\n",
    "\n",
    "### Create the world"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 1 0]\n",
      " [0 0 0 1 0]\n",
      " [0 1 1 0 0]\n",
      " [0 0 0 0 1]\n",
      " [1 0 0 1 0]\n",
      " [1 0 0 0 0]]\n",
      "\n",
      "The shape of this array is: (6, 5)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# A 6x5 robot world\n",
    "world = np.array([ [0, 0, 0, 1, 0],\n",
    "                   [0, 0, 0, 1, 0],\n",
    "                   [0, 1, 1, 0, 0],\n",
    "                   [0, 0, 0, 0, 1],\n",
    "                   [1, 0, 0, 1, 0],\n",
    "                   [1, 0, 0, 0, 0] ])\n",
    "\n",
    "# Print out some information about the world\n",
    "print(world)\n",
    "print('\\nThe shape of this array is: ' + str(world.shape))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Iterate through the items in the world"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "Index [0][0] = 0\n",
      "Index [0][1] = 0\n",
      "Index [0][2] = 0\n",
      "Index [0][3] = 1\n",
      "Index [0][4] = 0\n",
      "Index [1][0] = 0\n",
      "Index [1][1] = 0\n",
      "Index [1][2] = 0\n",
      "Index [1][3] = 1\n",
      "Index [1][4] = 0\n",
      "Index [2][0] = 0\n",
      "Index [2][1] = 1\n",
      "Index [2][2] = 1\n",
      "Index [2][3] = 0\n",
      "Index [2][4] = 0\n",
      "Index [3][0] = 0\n",
      "Index [3][1] = 0\n",
      "Index [3][2] = 0\n",
      "Index [3][3] = 0\n",
      "Index [3][4] = 1\n",
      "Index [4][0] = 1\n",
      "Index [4][1] = 0\n",
      "Index [4][2] = 0\n",
      "Index [4][3] = 1\n",
      "Index [4][4] = 0\n",
      "Index [5][0] = 1\n",
      "Index [5][1] = 0\n",
      "Index [5][2] = 0\n",
      "Index [5][3] = 0\n",
      "Index [5][4] = 0\n"
     ]
    }
   ],
   "source": [
    "# This function uses nested for loops and knowledge\n",
    "# about the shape of the array to print out each item with it's index\n",
    "def iterate2D(world):\n",
    "    # y-dimension (rows)\n",
    "    for i in range(0, world.shape[0]):\n",
    "        # x-dimension (columns)\n",
    "        for j in range(0, world.shape[1]):\n",
    "            print('Index ['+str(i)+']['+str(j)+'] = ' +str(world[i][j]))\n",
    "\n",
    "# Call the iterate function\n",
    "print('\\n')\n",
    "iterate2D(world)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Find the first tree, `1`, in the world"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "First tree found at location: [0][3]\n"
     ]
    }
   ],
   "source": [
    "# This function is similar to our iterate2D function,\n",
    "# But looks for the first tree in the array and prints its location [x][y]\n",
    "def first_tree(world):\n",
    "    # iterates through all indices starting at the top-left [0][0]\n",
    "    for i in range(0, world.shape[0]):\n",
    "        for j in range(0, world.shape[1]):\n",
    "            # check if a tree is found\n",
    "            if(world[i][j] == 1):\n",
    "                # if so, print the index and leave the loop with a return statement\n",
    "                print('First tree found at location: ['+str(i)+']['+str(j)+']')\n",
    "                return \n",
    "\n",
    "            \n",
    "# Call the first_tree function\n",
    "print('\\n')\n",
    "first_tree(world)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
